<?php

namespace Api\Model;

use Think\Model;

class UserBusinessDistrictModel extends Model {

    /**
     * @name 保存选择的商区
     */
    public function saveBusinessDistrict($userid, $businessDistricts = array ()) {
        $this->where("`user_id`={$userid}")->delete();
        foreach ($businessDistricts as $key => $val) {
            $data = array ('user_id' => $userid, 'business_district_id' => $val);
            $this->add($data);
        }
    }

    /**
     * @name 获取用户商区
     */
    public function getUserBusinessDistrict($userid) {
        $city = null;
        $businessDistrictIds = $this->where("`user_id`={$userid}")->getField('business_district_id', true);
        if ($businessDistrictIds) {
            $cityId=M('Area')->where("`id`={$businessDistrictIds[0]}")->getField('parentid');
            $city = M('Area')->where("`id`={$cityId}")->field('id,areaname')->find();
        } else {
            $businessDistrictIds = array ();
        }
        return array ('city_id' => $city['id'], 'city_name' => $city['areaname'], 'business_district_id' => $businessDistrictIds);
    }


}